/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.bricket.plugin.user.web;

import org.apache.wicket.extensions.validation.validator.RfcCompliantEmailAddressValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;

import com.googlecode.bricket.plugin.user.domain.User;

public class AccountPanel extends Panel {
	private final Model<String> passwordModel = new Model<String>("");

	public AccountPanel(String id, IModel<User> model, Form<User> form) {
		super(id, model);
		StringResourceModel lEmail = new StringResourceModel("email.label", AccountPanel.this, null);
		add(new Label("email.label", lEmail));
		add(new TextField<String>("email", String.class)
				.setRequired(true)
				.setLabel(lEmail)
				.add(RfcCompliantEmailAddressValidator.getInstance()));

		StringResourceModel lPwd = new StringResourceModel("password.label", AccountPanel.this, null);
		add(new Label("password.label", lPwd));
		PasswordTextField password = new PasswordTextField("password");
		password.setModel(passwordModel);
		password.setRequired(false).setLabel(lPwd);
		add(password);

		StringResourceModel lPwdrep = new StringResourceModel("passwordrepeat.label", AccountPanel.this, null);
		add(new Label("passwordrepeat.label", lPwdrep));
		PasswordTextField passwordrepeat = new PasswordTextField("passwordrepeat");
		passwordrepeat.setModel(password.getModel());
		passwordrepeat.setRequired(false).setLabel(lPwdrep);
		add(passwordrepeat);
		
		form.add(new EqualPasswordInputValidator(password, passwordrepeat));

		StringResourceModel lFname = new StringResourceModel("firstname.label", AccountPanel.this, null);
		add(new Label("firstname.label", lFname));
		add(new TextField<String>("firstname", String.class).setRequired(true).setLabel(lFname));

		StringResourceModel lLname = new StringResourceModel("lastname.label", AccountPanel.this, null);
		add(new Label("lastname.label", lLname));
		add(new TextField<String>("lastname", String.class).setRequired(true).setLabel(lLname));
	}

	public void processPassword() {
		String password = passwordModel.getObject();
		if (!"".equals(password)) {
			((User) getDefaultModelObject()).setPassword(password);
		}
	}
}
